package com.vueblog.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.vueblog.po.User;
import com.vueblog.service.TokenService;
import com.vueblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;

    @PostMapping("/adminLogin")
    public Object adminLogin(@RequestBody User user){
        JSONObject jsonObject = new JSONObject();
        User u = userService.findByUsernameAndPwd(user);
        if (u != null){
            String token = tokenService.getToken(u);
            u.setPassword(null);
            jsonObject.put("token",token);
            jsonObject.put("user",u);
            jsonObject.put("msg","管理员登录");
            jsonObject.put("code",200);
        }else{
            jsonObject.put("code",201);
            jsonObject.put("msg","账号或密码错误");
        }
        return jsonObject;
    }
}
